############################################################
# Appendix A — Figures & Tables (A1–A11)
# Output dir:  ~/Dropbox/Issue voting Chile/09_replication/output
############################################################

rm(list = ls())

# ---- Packages ----
suppressPackageStartupMessages({
  library(haven)
  library(dplyr)
  library(forcats)
  library(ggplot2)
  library(scales)
  library(tidyr)
  library(stringr)
  library(xtable)
  library(cregg)
  library(gridExtra)
})

# ---- Paths ----
base_dir <- "~/Dropbox/Issue voting Chile"
outdir   <- file.path(base_dir, "09_replication/output")
dir.create(outdir, showWarnings = FALSE, recursive = TRUE)

# ---- Plot saver (always white bg) ----
save_fig <- function(filename, plot, width = 6, height = 4.5, dpi = 300) {
  ggsave(filename, plot = plot, width = width, height = height, dpi = dpi, bg = "white")
}

# =========================================================
# Helper: map A12 (1–10 / “No sé/ Ninguna”) → ordered scale + “None”
# =========================================================
recode_A12_to_cat <- function(x) {
  # normalize "1 Izquierda", "10 Derecha", "No sé/ Ninguna", ".", etc.
  x_chr <- as.character(x)
  x_chr <- ifelse(grepl("^1(\\s|$)", x_chr) | grepl("Izquierda", x_chr), "1",  x_chr)
  x_chr <- ifelse(grepl("^10(\\s|$)",x_chr) | grepl("Derecha",   x_chr), "10", x_chr)
  x_chr <- ifelse(grepl("No s", x_chr, ignore.case = TRUE) | x_chr %in% c(".", "", "NA"), NA, x_chr)
  
  lab <- dplyr::case_when(
    x_chr == "1"  ~ "Left",
    x_chr == "2"  ~ "2",
    x_chr == "3"  ~ "3",
    x_chr == "4"  ~ "4",
    x_chr == "5"  ~ "5",
    x_chr == "6"  ~ "6",
    x_chr == "7"  ~ "7",
    x_chr == "8"  ~ "8",
    x_chr == "9"  ~ "9",
    x_chr == "10" ~ "Right",
    is.na(x_chr)  ~ NA_character_
  )
  
  fct_explicit_na(
    factor(lab, levels = c("Left","2","3","4","5","6","7","8","9","Right")),
    na_level = "None"
  )
}

# ---- Color palettes ----
# A1/A3 bars
ideol_bar_pal <- c(
  "Left"  = "#f2f0f7", "2" = "#dadaeb", "3" = "#bcbddc", "4" = "#9e9ac8",
  "5"     = "#807dba", "6" = "#6a51a3", "7" = "#54278f", "8" = "#3f007d", 
  "9"     = "darkslateblue",  "Right" = "#3f007d", "None" = "gray20"
)
# A2 lines
issues_pal <- c("#990000", "#003399", "#006600", "black", "darkslateblue", "gray")
# A4 state ownership (match appendix look: Blue=Agree, Red=Disagree, Gray=Neutral, Light gray=None)
state_pal <- c(
  "Agreed" = "#003399",
  "Disagreed" = "#990000",
  "Neutral / Don't Know" = "gray40",
  "None" = "gray80"
)

# =========================================================
# Load panel data (long format) once
#   Expect: panel_long.dta with columns: wave, A12_wave, panel_all, estado_dueno, a6e, left/right, etc.
# =========================================================
panel_path_main <- file.path(base_dir, "09_replication/data/panel_long.dta")
panel_path_alt1 <- file.path(base_dir, "01_data/clean_data/panel_long.dta")
panel_path_alt2 <- file.path(base_dir, "01_data/clear_data/panel_long.dta")
panel_long <- if (file.exists(panel_path_main)) read_dta(panel_path_main) else
  if (file.exists(panel_path_alt1)) read_dta(panel_path_alt1) else read_dta(panel_path_alt2)

if (!"wave" %in% names(panel_long)) stop("`wave` not found in panel_long.")
if (!"A12_wave" %in% names(panel_long)) stop("`A12_wave` not found in panel_long.")
if (!"panel_all" %in% names(panel_long)) panel_long$panel_all <- 1L

# =========================================================
# A1. Histogram (Wave 1), include “None”; y = %
# =========================================================
df_A1 <- panel_long %>%
  filter(wave == 1) %>%
  mutate(ideol_cat = recode_A12_to_cat(A12_wave)) %>%
  count(ideol_cat, .drop = FALSE) %>%
  mutate(pct = 100 * n / sum(n))

fig_A1 <- ggplot(df_A1, aes(x = ideol_cat, y = pct, fill = ideol_cat)) +
  geom_col(color = "black", width = 0.9) +
  scale_fill_manual(values = ideol_bar_pal, guide = "none") +
  scale_y_continuous(labels = function(x) paste0(x, "%")) +
  labs(x = "Ideology", y = "Percentage", title = "Distribution of Ideology — Wave 1") +
  theme_minimal(base_size = 12) +
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    plot.title  = element_text(hjust = 0.5)
  )
save_fig(file.path(outdir, "A1_histogram_wave1.png"), fig_A1, width = 7.5, height = 4.8)

# =========================================================
# A3. Overlay distribution: Waves 2 vs 3, merged respondents (panel_all==1)
#   - keep only ids present in BOTH waves 2 and 3
#   - include explicit “None”
# =========================================================
# choose an id column
id_candidates <- c("CodPanelista","numericalid","id","ID","respondent_id")
rid <- id_candidates[id_candidates %in% names(panel_long)]
if (length(rid) == 0) { panel_long$RID <- seq_len(nrow(panel_long)); rid <- "RID" } else { rid <- rid[1] }

merged_ids <- panel_long %>%
  filter(panel_all == 1, wave %in% c(2,3)) %>%
  distinct(!!sym(rid), wave) %>%
  count(!!sym(rid)) %>%
  filter(n == 2) %>%
  pull(!!sym(rid))

df_A3 <- panel_long %>%
  filter(wave %in% c(2,3), !!sym(rid) %in% merged_ids) %>%
  mutate(ideol_cat = recode_A12_to_cat(A12_wave))

cats <- levels(recode_A12_to_cat(factor(c("1","2","3","4","5","6","7","8","9","10", NA))))
pct_A3 <- df_A3 %>%
  group_by(wave, ideol_cat) %>%
  summarise(n = n(), .groups = "drop") %>%
  group_by(wave) %>%
  complete(ideol_cat = cats, fill = list(n = 0)) %>%
  mutate(pct = 100 * n / sum(n)) %>%
  ungroup() %>%
  mutate(ideol_cat = factor(ideol_cat, levels = c("Left","2","3","4","5","6","7","8","9","Right","None")))

pct_w2 <- pct_A3 %>% filter(wave == 2)
pct_w3 <- pct_A3 %>% filter(wave == 3)

fig_A3 <- ggplot() +
  geom_col(data = pct_w2, aes(x = ideol_cat, y = pct),
           fill = "#003399", alpha = 0.55, color = "black", width = 0.9) +
  geom_col(data = pct_w3, aes(x = ideol_cat, y = pct),
           fill = "#990000", alpha = 0.55, color = "black", width = 0.6) +
  scale_y_continuous(labels = function(x) paste0(x, "%")) +
  labs(x = "Ideology", y = "Percentage",
       title = "Overlay Ideology Distribution — Merged Respondents (Wave 2 vs 3)",
       subtitle = "Blue = 2021 (Wave 2), Red = 2023 (Wave 3)") +
  theme_minimal(base_size = 12) +
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    plot.title  = element_text(hjust = 0.5)
  )
save_fig(file.path(outdir, "A3_overlay_w2_w3_merged.png"), fig_A3, width = 8, height = 5)

# =========================================================
# A4. State ownership (stacked % bars), Waves 2 & 3, panel_all==1
#   - Variable: estado_dueno (explicit “None” for NA)
#   - Keep palette consistent with appendix
# =========================================================
df_A4 <- panel_long %>%
  filter(panel_all == 1, wave %in% c(2,3)) %>%
  mutate(estado_dueno = fct_explicit_na(estado_dueno, na_level = "None"),
         estado_dueno = fct_relevel(estado_dueno, "Agreed","Disagreed","Neutral / Don't Know","None"))

pct_A4 <- df_A4 %>%
  group_by(wave, estado_dueno) %>%
  summarise(n = n(), .groups = "drop") %>%
  group_by(wave) %>%
  mutate(pct = n / sum(n)) %>%
  ungroup()

fig_A4 <- ggplot(pct_A4, aes(x = factor(wave), y = pct, fill = estado_dueno)) +
  geom_col(color = "black") +
  geom_text(aes(label = paste0(round(100 * pct, 1), "%")),
            position = position_stack(vjust = 0.5),
            size = 3.8, color = "white", show.legend = FALSE) +
  scale_fill_manual(values = state_pal, name = NULL, na.translate = FALSE) +
  scale_x_discrete(labels = c("2" = "2021", "3" = "2023")) +
  scale_y_continuous(labels = percent_format(accuracy = 1)) +
  labs(x = "Year", y = "", title = "Should the State Nationalize Main Companies?") +
  theme_minimal(base_size = 12) +
  theme(
    panel.border = element_rect(color = "gray", fill = NA, linewidth = 0.8),
    plot.title   = element_text(hjust = 0.5)
  )
save_fig(file.path(outdir, "A4_state_ownership.png"), fig_A4, width = 7, height = 5)

# =========================================================
# A2. CEP “priority issues” figure + compact LaTeX table
#   Data: base_issue.dta  (encuesta_a + issue_3/5/7/8/9/12 in 0–1)
# =========================================================
cep_issue_path_main <- file.path(base_dir, "09_replication/data/base_issue.dta")
cep_issue_path_alt1 <- file.path(base_dir, "01_data/clean_data/base_issue.dta")
cep_issue_path_alt2 <- file.path(base_dir, "01_data/clear_data/base_issue.dta")
cep <- if (file.exists(cep_issue_path_main)) read_dta(cep_issue_path_main) else
  if (file.exists(cep_issue_path_alt1)) read_dta(cep_issue_path_alt1) else read_dta(cep_issue_path_alt2)

if (!"encuesta_a" %in% names(cep)) stop("`encuesta_a` not found in CEP issues data.")

issue_vars <- c(
  crime      = "issue_5",
  education  = "issue_7",
  poverty    = "issue_9",
  jobs       = "issue_8",
  pensions   = "issue_3",
  healthcare = "issue_12"
)

long_list <- list()
for (nm in names(issue_vars)) {
  v <- issue_vars[[nm]]
  if (!v %in% names(cep)) next
  tmp <- cep %>%
    group_by(encuesta_a) %>%
    summarise(Percentage = mean(.data[[v]], na.rm = TRUE)) %>%
    mutate(issue = nm)
  long_list[[nm]] <- tmp
}
issue_all <- bind_rows(long_list) %>%
  mutate(issue = factor(issue,
                        levels = c("crime","education","poverty","jobs","pensions","healthcare"),
                        labels = c("Crime","Education","Poverty","Jobs","Pensions","Health Care")))

fig_A2 <- ggplot(issue_all, aes(x = encuesta_a, y = Percentage, color = issue, group = issue)) +
  geom_line(linewidth = 1.0) +
  scale_color_manual(values = issues_pal, name = NULL) +
  scale_x_continuous(breaks = scales::pretty_breaks()) +
  scale_y_continuous(labels = percent_format(accuracy = 1)) +
  labs(x = "Year", y = "Priority Issues (%)", title = "CEP: Priority Issues Over Time (1994–2023)") +
  theme_minimal(base_size = 12) +
  theme(
    legend.text = element_text(size = 11),
    plot.title  = element_text(hjust = 0.5)
  )
save_fig(file.path(outdir, "A2_CEP_issues_plot.png"), fig_A2, width = 8, height = 5)

## A5 - A11


############################################################
# Appendix A — Conjoint (A5–A9 + A8/A6 interactions + A10)
# Uses replication directories; logic/plots unchanged
# Output dir:  ~/Dropbox/Issue voting Chile/09_replication/output
############################################################

rm(list = ls())

# ---- Packages ----
library(foreign)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)
library(readxl)
library(cregg)
library(ggplot2)
library(gridExtra)
library(readstata13)
library(haven)
library(dplyr)
library(forcats)
library(ggeasy)

# ---- Paths ----
base_dir <- "~/Dropbox/Issue voting Chile"
outdir   <- file.path(base_dir, "09_replication/output")
dir.create(outdir, showWarnings = FALSE, recursive = TRUE)

# Helper: read wave2_conjoint from replication folder, fallback to original
w2_path_main <- file.path(base_dir, "09_replication/data/wave2_conjoint.dta")
w2_path_alt  <- file.path(base_dir, "01_data/clean_data/wave2_conjoint.dta")

# ---- Load waves ----
wave2_conjoint <- if (file.exists(w2_path_main)) {
  read_dta(w2_path_main)
} else {
  read_dta(w2_path_alt)
}

atr <- c("a_1","a_2","a_3","a_4", "a_5", "a_6")
wave2_conjoint[, atr] <- lapply(wave2_conjoint[, atr], factor)
wave2_conjoint$Ideology   <- wave2_conjoint$a_1
wave2_conjoint$Gender     <- wave2_conjoint$a_2
wave2_conjoint$Age        <- wave2_conjoint$a_3
wave2_conjoint$Feminism   <- wave2_conjoint$a_4
wave2_conjoint$Immigration<- wave2_conjoint$a_5
wave2_conjoint$Crime      <- wave2_conjoint$a_6

# -----------------------------
# A9 — AMCE (ALL, non-interacted)
# -----------------------------
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology,
                                  levels = c("Right", "Left"))
wave2_conjoint$Feminism <- factor(wave2_conjoint$Feminism,
                                  levels = c("Non-Feminist", "Feminist"))
wave2_conjoint$Crime <- factor(wave2_conjoint$Crime,
                               levels = c("No Harsher Punishment", "Harsher Punishment"))
wave2_conjoint$Immigration <- factor(wave2_conjoint$Immigration,
                                     levels = c("No Restrictions", "New Immigration Restrictions"))

m1     <- choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime
amce1  <- cj(wave2_conjoint, m1, id = ~ numericalid, estimate = "amce")
plot1  <- plot(amce1)

plot1 <- plot1 +
  ggplot2::theme_bw() +
  scale_color_manual(values = rep("black", 9)) +
  guides(color = FALSE) +
  theme(
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 10, color = "black"),
    axis.title.x = element_blank()
  ) +
  scale_x_continuous(
    limits = c(-0.1, 0.4),
    breaks = c(-0.1, 0, 0.1, 0.2, 0.3, 0.4)
  )

plot1
ggsave(file.path(outdir, "amce_all.png"), plot1, width = 5, height = 5.4, bg = "white")

# -----------------------------
# A5 — Right anti-immigration subsample (AMCE)
# -----------------------------
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology,
                                  levels = c("Left", "Right"))

right_mig <- subset(wave2_conjoint, right_anti_mig == 1)

amce3  <- cj(right_mig, choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime,
             id = ~ numericalid, estimate = "amce")
plot3  <- plot(amce3[c(1, 2, 10, 11), ])

plot3 <- plot3 +
  ggplot2::theme_bw() +
  scale_color_manual(values = rep("black", 9)) +
  guides(color = FALSE) +
  theme(
    axis.text.x = element_text(size = 10, color = "black"),
    axis.text.y = element_text(size = 10, color = "black"),
    axis.title.x = element_blank()
  ) +
  scale_x_continuous(
    limits = c(-0.1, 0.6),
    breaks = seq(-0.1, 0.6, 0.2)
  ) +
  labs(title = "Right Anti-immigration Subsample") +
  easy_center_title() +
  theme(
    plot.title = element_text(size = 12),
    axis.text.x = element_text(size = 10),
    axis.text.y = element_text(size = 10)
  )

plot3
ggsave(file.path(outdir, "right_mig.png"), plot3, width = 5, height = 5.4, bg = "white")

# -----------------------------
# A6 — Interaction Right × Immigration (by = Immigration)
# -----------------------------
amces_int2   <- cj(right_mig, choice_clean2 ~ Ideology + Gender + Age + Feminism + Crime,
                   id = ~ numericalid, estimate = "amce", by = ~ Immigration)
diff_amces2  <- cj(right_mig, choice_clean2 ~ Ideology + Gender + Age + Feminism + Crime,
                   id = ~ numericalid, estimate = "amce_diff", by = ~ Immigration)

plot5 <- plot(rbind(amces_int2[c(1, 2, 12, 13), ], diff_amces2[6, ])) +
  ggplot2::facet_wrap(~BY, ncol = 3L)

plot5 <- plot5 +
  ggplot2::theme_bw() +
  scale_color_manual(values = rep("black", 9)) +
  guides(color = FALSE) +
  theme(
    axis.text.x = element_text(size = 10, color = "black"),
    axis.title.x = element_blank()
  ) +
  scale_x_continuous(
    limits = c(-0.1, 0.6),
    breaks = seq(-0.1, 0.6, 0.2)
  ) +
  labs(title = "Interaction by Anti-Immigration Attribute") +
  easy_center_title() +
  theme(
    plot.title = element_text(size = 11),
    axis.text.x = element_text(size = 10),
    axis.text.y = element_text(size = 10)
  )

plot5
ggsave(file.path(outdir, "right_mig_int.png"), plot5, width = 7, height = 5, bg = "white")

# -----------------------------
# A7 — Left pro-immigration subsample (AMCE)
# -----------------------------
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology,
                                  levels = c("Right", "Left"))

left_mig <- subset(wave2_conjoint, left_pro_mig == 1)

amce3  <- cj(left_mig, choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime,
             id = ~ numericalid, estimate = "amce")
plot3  <- plot(amce3[c(1, 2, 10, 11), ])

plot3 <- plot3 +
  ggplot2::theme_bw() +
  scale_color_manual(values = rep("black", 9)) +
  guides(color = FALSE) +
  theme(
    axis.text.x = element_text(size = 10, color = "black"),
    axis.text.y = element_text(size = 10, color = "black"),
    axis.title.x = element_blank()
  ) +
  scale_x_continuous(
    limits = c(-0.1, 0.6),
    breaks = seq(-0.1, 0.6, 0.2)
  ) +
  labs(title = "Left Pro-immigration Subsample") +
  easy_center_title() +
  theme(
    plot.title = element_text(size = 12),
    axis.text.x = element_text(size = 10),
    axis.text.y = element_text(size = 10)
  )

plot3
ggsave(file.path(outdir, "left_mig.png"), plot3, width = 5, height = 5.4, bg = "white")

# -----------------------------
# A8 — Interaction Left × Immigration (by = Immigration)
# -----------------------------
amces_int2   <- cj(left_mig, choice_clean2 ~ Ideology + Gender + Age + Feminism + Crime,
                   id = ~ numericalid, estimate = "amce", by = ~ Immigration)
diff_amces2  <- cj(left_mig, choice_clean2 ~ Ideology + Gender + Age + Feminism + Crime,
                   id = ~ numericalid, estimate = "amce_diff", by = ~ Immigration)

plot5 <- plot(rbind(amces_int2[c(1, 2, 12, 13), ], diff_amces2[6, ])) +
  ggplot2::facet_wrap(~BY, ncol = 3L)

plot5 <- plot5 +
  ggplot2::theme_bw() +
  scale_color_manual(values = rep("black", 9)) +
  guides(color = FALSE) +
  theme(
    axis.text.x = element_text(size = 10, color = "black"),
    axis.title.x = element_blank()
  ) +
  scale_x_continuous(
    limits = c(-0.2, 0.6),
    breaks = seq(-0.2, 0.6, 0.2)
  ) +
  labs(title = "Interaction by Anti-Immigration Attribute") +
  easy_center_title() +
  theme(
    plot.title = element_text(size = 11),
    axis.text.x = element_text(size = 10),
    axis.text.y = element_text(size = 10)
  )

plot5
ggsave(file.path(outdir, "left_mig_int.png"), plot5, width = 7, height = 5, bg = "white")

# -----------------------------
# A10 — Marginal Means (ALL)
# -----------------------------
# (Reload from replication/fallback exactly as your working code did)
wave2_conjoint <- if (file.exists(w2_path_main)) {
  read_dta(w2_path_main)
} else {
  read_dta(w2_path_alt)
}

atr <- c("a_1","a_2","a_3","a_4", "a_5", "a_6")
wave2_conjoint[, atr] <- lapply(wave2_conjoint[, atr], factor)
wave2_conjoint$Ideology    <- wave2_conjoint$a_1
wave2_conjoint$Gender      <- wave2_conjoint$a_2
wave2_conjoint$Age         <- wave2_conjoint$a_3
wave2_conjoint$Feminism    <- wave2_conjoint$a_4
wave2_conjoint$Immigration <- wave2_conjoint$a_5
wave2_conjoint$Crime       <- wave2_conjoint$a_6

wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology,
                                  levels = c("Right", "Left"))
wave2_conjoint$Feminism <- factor(wave2_conjoint$Feminism,
                                  levels = c("Non-Feminist", "Feminist"))
wave2_conjoint$Crime <- factor(wave2_conjoint$Crime,
                               levels = c("No Harsher Punishment", "Harsher Punishment"))
wave2_conjoint$Immigration <- factor(wave2_conjoint$Immigration,
                                     levels = c("No Restrictions", "New Immigration Restrictions"))

wave2_conjoint <- wave2_conjoint %>%
  mutate(Immigration = recode(Immigration, "New Immigration Restrictions" = "New Restrictions"),
         Crime       = recode(Crime,
                              "No Harsher Punishment" = "Less Punitive",
                              "Harsher Punishment"    = "More Punitive"))

m2   <- choice_clean2 ~ Ideology + Gender + Age + Feminism + Immigration + Crime
mm1  <- cj(wave2_conjoint, m2, id = ~ numericalid, estimate = "mm")
plot6 <- plot(mm1, vline = 0.5)

plot6 <- plot6 +
  ggplot2::theme_bw() +
  scale_color_manual(values = rep("black", 9)) +
  guides(color = FALSE) +
  theme(
    axis.text.x = element_text(size = 12, color = "black"),
    axis.text.y = element_text(size = 10, color = "black"),
    axis.title.x = element_blank()
  ) +
  scale_x_continuous(
    limits = c(0.3, 0.7),
    breaks = c(0.3, 0.4, 0.5, 0.6, 0.7)
  )

plot6
ggsave(file.path(outdir, "mm_all.png"), plot6, width = 5, height = 5.4, bg = "white")


